package net.orange.framework.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import net.orange.architecture.model.BaseValueObject;
import net.orange.architecture.validator.NotDulicateValidator;
import net.orange.architecture.validator.StringNotNullValidator;
import net.orange.architecture.validator.Validator;
import net.orange.console.SystemConsole;
import net.orange.console.schedule.Schedule;

/**
 * The system schedule job, it can run the the special time period. It can use
 * for data maintain, transfer or other special business task. Its program can
 * be write in Java dynamic script.
 */
@Entity
@Table(name = "t_job")
public class Job extends BaseValueObject {

	private static final long serialVersionUID = -5778818840338427842L;

	/**
	 * The job code.
	 */
	@Column(name = "code")
	protected String code;

	/**
	 * The job name
	 */
	@Column(name = "name")
	protected String name;

	/**
	 * The job's program
	 */
	@Column(name = "program")
	protected String program;

	/**
	 * The description
	 */
	@Column(name = "description")
	protected String description;

	/**
	 * The job start time.
	 */
	protected Date starttime;

	/**
	 * The execution time interval
	 */
	protected long interval_hour;

	/**
	 * The job status - running of stop.
	 */
	protected String status;

	/**
	 * The time of last run
	 */
	protected Date lastrun;

	/**
	 * The total run times.
	 */
	protected int runtimes;

	/**
	 * @return Returns the code.
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            The code to set.
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return Returns the description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            The description to set.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return Returns the interval_hour.
	 */
	public long getInterval_hour() {
		return interval_hour;
	}

	/**
	 * @param interval_hour
	 *            The interval_hour to set.
	 */
	public void setInterval_hour(long interval_hour) {
		this.interval_hour = interval_hour;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return Returns the program.
	 */
	public String getProgram() {
		return program;
	}

	/**
	 * @param program
	 *            The program to set.
	 */
	public void setProgram(String program) {
		this.program = program;
	}

	/**
	 * @return Returns the starttime.
	 */
	public Date getStarttime() {
		return starttime;
	}

	/**
	 * @param starttime
	 *            The starttime to set.
	 */
	public void setStarttime(Date startdate) {
		this.starttime = startdate;
	}

	/**
	 * @return Returns the status.
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            The status to set.
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return The interval (sceond)
	 */
	public long getInterval_second() {
		return interval_hour * 1000 * 60;
	}

	/**
	 * @return Returns the running.
	 */
	public boolean isRunning() {
		return Schedule.isRunning(this);
	}

	/**
	 * @return Returns the lastrun.
	 */
	public Date getLastrun() {
		return lastrun;
	}

	/**
	 * @param lastrun
	 *            The lastrun to set.
	 */
	public void setLastrun(Date lastrun) {
		this.lastrun = lastrun;
	}

	/**
	 * @return Returns the runtimes.
	 */
	public int getRuntimes() {
		return runtimes;
	}

	/**
	 * @param runtimes
	 *            The runtimes to set.
	 */
	public void setRuntimes(int times) {
		this.runtimes = times;
	}

	/**
	 * Execute the job.
	 * 
	 * @throws Exception
	 */
	public void execute() throws Exception {
		if (program != null && program.length() > 0) {
			SystemConsole console = new SystemConsole();
			console.execute(program);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.model.BaseValueObject#satisfy(java.util.Collection)
	 */
	public boolean satisfy(Collection<String> msg) throws Exception {

		if (msg == null)
			msg = new ArrayList<String>();

		Validator validator = new Validator(msg);
		validator.add(new StringNotNullValidator(getCode(), "job. code"));
		validator.add(new StringNotNullValidator(getName(), "job. name"));
		validator.add(new NotDulicateValidator(this, "code", "code"));
		validator.validate();

		return (msg == null || msg.size() <= 0);
	}
}
